package com.bq.gametrade.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;

/**
 * Created by feel-wu on 2015/3/19.
 */
public class MyRadioGroup extends LinearLayout {

    private Context c;
    private OnItemChangedListener onItemChangedListener;
    private ArrayList<MyRadioButton> radioButtons;
    private int currentButtonId = 0;


    public MyRadioGroup(Context context) {
        this(context, null);
    }

    public MyRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        c = context;
        setOrientation(HORIZONTAL);
    }

    private void initItems() {
        if (radioButtons != null && !radioButtons.isEmpty()) {
            for (int i = 0; i < radioButtons.size(); i++) {
                final MyRadioButton radioButton = radioButtons.get(i);
                if (i == 0)
                    radioButton.setChecked(true);
                View view = radioButton.item_root;
                view.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setCheckedIndex(radioButton);
                    }
                });
                addView(view);
            }
        }
    }

    private void setCheckedIndex(MyRadioButton radioButton) {
        if (currentButtonId != radioButton.radio_group_item_id) {
            changeButtonState(radioButton);
            if (onItemChangedListener != null)
                onItemChangedListener.OnItemChanged(radioButton.radio_group_item_id);
        }
    }

    /**
     * 设置按钮
     *
     * @param radioButtons 设置
     */
    public void setRadioButtons(ArrayList<MyRadioButton> radioButtons) {
        this.radioButtons = radioButtons;
        initItems();
    }

    public void setRadioButtonChecked(int i) {
        if (currentButtonId != i) {
            changeButtonState(radioButtons.get(i));
            currentButtonId = i;
        }
    }

    private void changeButtonState(MyRadioButton radioButton) {
        radioButton.setChecked(true);
        radioButtons.get(currentButtonId).setChecked(false);
        currentButtonId = radioButton.radio_group_item_id;
    }

    /**
     * 设置按钮点击监听器
     *
     * @param onItemChangedListener 按钮点击监听器
     */
    public void setOnItemChangedListener(OnItemChangedListener onItemChangedListener) {
        this.onItemChangedListener = onItemChangedListener;
    }

    /**
     * 得到当前按钮ID
     *
     * @return 当前按钮ID
     */
    public int getCurrentButtonId() {
        return currentButtonId;
    }

    /**
     * 页面滑动时候，按钮的透明度变化
     *
     * @param i         当前按钮ID
     * @param alpha     当前按钮normal的透明度
     * @param direction 滑动方向，false=左，true=右，null=不动
     */
    public void setRadioButtonAlpha(int i, float alpha, Boolean direction) {
        if (direction != null) {
            if (direction) {
                //右
                setRadioButtonAlpha(i,i+1,alpha);
            } else {
                //左
                setRadioButtonAlpha(i-1,i,alpha);
            }
        }
    }

    /**
     * 两个按钮改变透明度
     *
     * @param leftIndex  左边的按钮索引
     * @param rightIndex 右边的按钮索引
     * @param alpha      [0,1)透明度
     */
    public void setRadioButtonAlpha(int leftIndex, int rightIndex, float alpha) {
        if (leftIndex < 0 || rightIndex < 0 || leftIndex >= radioButtons.size() || rightIndex >= radioButtons.size())
            return;
        radioButtons.get(leftIndex).setButtonNormalAlpha(1f - alpha);
        radioButtons.get(rightIndex).setButtonNormalAlpha(alpha);
    }

    /**
     * 按钮改变事件回调接口
     */
    public interface OnItemChangedListener {
        /**
         * 按钮改变事件回调函数
         *
         * @param checkedId 选择条目ID
         */
        void OnItemChanged(int checkedId);
    }

}
